package jTorrent;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Array;
import java.net.URL;
import java.text.DecimalFormat;
import javax.swing.ImageIcon;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

public class XMLRPCFunctions
{
	XmlRpcClient client = new XmlRpcClient();
	ImageIcon redIcon = new ImageIcon(Misc.createStateIcon(Color.red, Symbol.PAUSED));
	ImageIcon greenIcon = new ImageIcon(Misc.createStateIcon(Color.green, Symbol.PLAY));
	
	public void estConnection(URL url, String user, String pass)
	{
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(url);
		config.setBasicUserName(user);
		config.setBasicPassword(pass);
		
	    client.setConfig(config);
	    client.setTypeFactory(new ModTypeFactory(client));
	}
	
	public String getURL()
	{
		return ((XmlRpcClientConfigImpl) client.getClientConfig()).getServerURL().toString();
	}
	
	public String getUser()
	{
		return ((XmlRpcClientConfigImpl) client.getClientConfig()).getBasicUserName();
	}
	
	public String getPass()
	{
		return ((XmlRpcClientConfigImpl) client.getClientConfig()).getBasicPassword();
	}
	
	public Object setCustom(String hash, int n)
	{
		Object result = "leer";
		Object[] params = {hash};
		
		try
		{
			result = client.execute("d.custom" + n, params);
		}
		catch (XmlRpcException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	
	public Object startTorrent(String hash)
	{
		Object result = "leer";
		Object[] params = {hash};
		
		try
		{
			result = client.execute("d.start", params);
		}
		catch (XmlRpcException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	
	public Object stopTorrent(String hash)
	{
		Object result = "leer";
		Object[] params = {hash};
		
		try
		{
			result = client.execute("d.stop", params);
		}
		catch (XmlRpcException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}

	public Object addTorrent(File file)
	{
		FileInputStream reader = null;
		Object result = "leer";
		try {
			byte[] source = new byte[(int)file.length()];
			reader = new FileInputStream(file);
			reader.read(source, 0, source.length);	
			
			Object[] params = {source};
			result = client.execute("load_raw_start", params);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (XmlRpcException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try
			{
				reader.close();
			}
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	public Object addTorrent(String url)
	{
	    Object[] params = {url};
	    Object result = "leer";
		try
		{
			result = client.execute("load_start", params);
		}
		catch (XmlRpcException e)
		{
			e.printStackTrace();
		}
		
		return result;
	}
	
	public Object deleteTorrent(String hash)
	{
		Object result = "leer";
		Object[] params = {hash};
		
		try
		{
			result = client.execute("d.erase", params);
		}
		catch (XmlRpcException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	
	public Object getTorrentList()
	{
		//System.out.println("getTorrentList() START");
		//Object[] params = null;
	    Object[] params = {"main", "d.get_hash=", "d.get_state=", "d.get_name=", "d.get_size_bytes=", "d.get_completed_bytes=", "d.get_up_total=", "d.get_down_rate=", "d.get_up_rate=", "d.get_ratio=", "d.get_custom1=", "d.get_custom2="};
	    Object result = "fehler";
		try
		{
			result = client.execute("d.multicall", params);
			//result = client.execute("download_list", params);
		}
		catch (XmlRpcException e)
		{
			e.printStackTrace();
			return result;
		}
		
		//TODO: Wenn die Werte leer sind gibt's Exceptions und das Programm klappt nicht. (Vermutlich nicht mehr relevant)
		if(result.getClass().isArray() && Array.getLength(result) > 0  && Array.get(result, 0).getClass().isArray())
		{
			Object[][] sResult = new Object[Array.getLength(result)][Array.getLength(Array.get(result, 0))];
			DecimalFormat df = new DecimalFormat("#.#");
			for (int i = 0; i < sResult.length; i++)
			{
				for(int u = 0; u < sResult[i].length; u++)
				{
					if(u == 1)
					{
						sResult[i][u] = (Integer.valueOf(String.valueOf(Array.get(Array.get(result, i), u)).trim()) == 1 ? greenIcon : redIcon);
					}
					else if(u > 2 && u < 6)
					{
						sResult[i][u] = Misc.calcFittingFileUnit(Double.valueOf(String.valueOf(Array.get(Array.get(result, i), u)).trim()), false);
					}
					else if(u == 6 || u == 7)
					{
						sResult[i][u] = Misc.calcFittingFileUnit(Double.valueOf(String.valueOf(Array.get(Array.get(result, i), u)).trim()), true);
					}
					else if(u == 8)
					{
						sResult[i][u] = df.format(Float.valueOf(String.valueOf(Array.get(Array.get(result, i), u)).trim()) / 1000);
					}
					else
					{
						sResult[i][u] = String.valueOf(Array.get(Array.get(result, i), u)).trim();
					}
				}
			}
			//System.out.println("getTorrentList() ENDE");
			return sResult;
		}
		else if(result.getClass().isArray() && Array.getLength(result) > 0)
		{
			String[] sResult = new String[Array.getLength(result)];
			for (int i = 0; i < sResult.length; i++)
			{
				sResult[i] = (String) Array.get(result, i);
			}
			return sResult;
		}
		else
		{
			//System.out.println("getTorrentList() ENDE");
			return null;
		}
	}
}